import { isReferee } from "@/func";
import { WereWolfPickNumberState } from "@/werewolf/pickNumberState/state";
import { ButtonPushAfterEvent, Player } from "@minecraft/server";
import { Game, GameComponent } from "@sapi-game/main";
import { DimensionIds, EntityTypeIds } from "@sapi-game/utils/vanila-data";

export class PickNumberClaasicView extends GameComponent<WereWolfPickNumberState> {
    override onAttach(): void {
        this.subscribe(Game.events.buttonPush, this.startButton.bind(this), {
            dimensionId: DimensionIds.Overworld,
            loc: [-90, -49, -101],
            sourceType: EntityTypeIds.Player,
        });
    }

    startButton(t: ButtonPushAfterEvent) {
        const p = t.source as Player;
        if (!isReferee(p)) {
            p.sendMessage("你不是裁判，无法开始游戏");
        } else {
            if (!this.state.checkPlayers()) {
                return this.context.players.title("人数过少，无法开始");
            }
            this.state.start();
        }
    }
}
